//定义默认数据，类似于小程序的page.data

//页面大小，默认10条
var pageSize = 10;
//定义总条数
var pageTotal = 0;
//当前页码，默认为1 
var pageIndex = 1;
//定义分类默认为第一项  typeIndex = index = 0
var classifyId = 6;
//定义当前用户id
var userId = 0;
var dataList = '';//最后接收数据的
//执行方法
$(function () {
	//获得用户id
	userId = $.GetRequest().userId;
	if(userId){//用户id存在
		window.localStorage.setItem("userId",userId);//缓存用户id
	}else{
		userId = window.localStorage.getItem("userId")//url没有参数则从缓存取值
    }
	 cityId=$.GetRequest().cityId;
    if(cityId){
        // getList()
    }else{
        $.toast('未获取分站城市的ID','text')
	}
	//获取type类型
	if($.getPara("type") != ''){
		classifyId = $.getPara("type");
	}
	//点击事件-快速点击
	FastClick.attach(document.body);
	//监听触底事件初始化
	$(".fac-type-rightList").infinite();
	//获取轮播图列表
	swiperImgList();
	// //获取列表，默认第一页
	factoryList();


});

//获取轮播图
function swiperImgList() {
	$.ajax({
		url:"/api/banner-ad/banner/index",
		type: "get", //提交方式 
		data: {
			type:5,//1首页、2学院、3协会、4媒体、5印厂、6分站首页、7最新资讯、8推荐资讯、9展会
			cityId:cityId,
		},
		success: function (res) {
			console.log(res);
			var img = res.data;//banner图数组
			//console.log(img, "tupian");
			//轮播图图片数组
			var swiperImgList = [];
			$.each(img, function (index, val) {
				if(val.url_type == 0){
					swiperImgList += '<div class="swiper-slide"><a class="goDetails" href="javascript:void(0);"><img src="'+val.img+'"/></a></div>'
				}else{
					swiperImgList += '<div class="swiper-slide"><a class="goDetails" onclick="urlHref('+val.url_value+')"><img src="'+val.img+'"/></a></div>';
				}
			})
			//插入数据
			$('#fac-swiper').html(swiperImgList);
			//初始化swiper
			var swiper = new Swiper('.swiper-container', {
				autoplay: 3000,
				loop: true,
				pagination: '.swiper-pagination',
				autoplayDisableOnInteraction: false,
			});
			//获取根据分类的ID获取分类列表数据
			// factoryList();
		},
		error:function(res){
			$.toast(res.msg,'text');
		}
	});
}



//获取分类列表 默认第一选项
function factoryList() {
	$.ajax({
		url:  "/api/press/category/list",
		type: "get", //提交方式 
		data: {},
		success: function (res) {
			var type = res.data;
			//console.log(type)
			//分类导航栏数组
			var typeList = [];
			$.each(type, function (index, val) {
				//console.log(val.id);
				if (val.id == classifyId) {
					//把获取的type值赋给id
					val.id = classifyId;
					getList(cityId,classifyId);  //获取第一个分类ID
					typeList += "<li class='fac-active' data-id='" + val.id + "'><a>" + val.name + "</a></li>";
				} else {
					typeList += "<li  data-id='" + val.id + "'><a>" + val.name + "</a></li>";
				}
			})
			//插入数据
			$('#factory-classify').html(typeList);
			//点击左侧tab
			$(".fac-btn-fl li").bind("click", function () {
				$('#pageLoading').show();
				$('#factory-rightli').children().remove();
				$(this).addClass("fac-active").siblings().removeClass('fac-active');
				dataList = '';
				classifyId = $(this).attr("data-id");
				// console.log(classifyId);
				loading = false;//可以滚动加载
				// 隐藏我是有底线的
				$('#pageNomore').addClass('displaynone');
				pageIndex = 1;
				getList(cityId,classifyId);
			});
			//获取根据分类的ID获取分类列表数据
		},
	});
}
//获取列表数据
function getList(cityId,categoryId) {
	$.ajax({
		url:  "/api/press/substation/list",
		type: "get", //提交方式 
		data: {
            cityId:cityId,
			categoryId: categoryId, //分类ID
			pageIndex: pageIndex,
			pageSize: pageSize
		},
		success: function (res) {
			// console.log(res)
			let list = res.data.list;//接收数据
			//空列表数组，用来接收加载数据
			let wordList = "";
			if(dataList.length == 0 && list.length ==0){//第一次加载且数据为空
				$('#factory-rightli').html('<div style="font-size:0.25rem;text-align:center;">暂无数据</div>');
				// loading = true;	
				$('#pageLoading').hide();
			}else{
				if(list.length>0){//有新数据
					// debugger;
					//遍历列表，拼接html字符串：下面的字段名与Mock自定义的字段名对应，后期需要修改为真实接口字段名
					$.each(list, function (index, val) {
						wordList += "<li>" + '<a onclick="urlHref('+val.id+')">'+
							"<div class=\"img\">" +
							"<img src=\"" + val.head_img + "\">" +
							"</div>" +
							"<div class=\"txt\">" +
							"<ul class=\"fac-fr-ul\">" +
							"<li>" +
							"<span class=\"txt1\">【公司】</span>" +
							"<span class=\"txt2\">" + val.company_name + "</span>" +
							"</li>" +
							"<li>" +
							"<span class=\"txt1\">【主营】</span>" +
							"<span class=\"txt2\">" + val.main_business + "</span>" +
							"</li>" +
							"<li>" +
							"<span class=\"txt1\">【地址】</span>" +
							"<span class=\"txt2\">" + val.address + "</span>" +
							"</li>" +
							"<li>" +
							"<span class=\"txt3\">联系电话：" + val.mobile + "</span>" +
							"</li>" +
							"</ul>" +
							"</div>" +
							"</a></li>";
					})
					// debugger;
					// console.log(wordList)
					dataList =  wordList;
				}else{
					$.toast("没有更多数据了...","text")
					$('#pageLoading').hide();//隐藏正在加载
					loading = true;	//隐藏加载中
					return false;
				}
				// debugger;	
				// console.log(dataList);
				$('#pageLoading').hide();	
				$('#factory-rightli').append(dataList);
			}
			loading = false;	
		}
	});
}
//列表跳转
function urlHref(item){
	var para = {};
	para.title = "企业库详情";
	para.type = "/enterpriseDetails";
	para.url = '/PageClassify/html/Substation/enterpriseDetails.html?id=' + item;
	// window.location.href =para.url;
	if (this.userId > 0) {
	    para.url = '/PageClassify/html/Substation/enterpriseDetails.html?id=' + item + "&userId=" + this.userId;
	}
	if (/(Android)/i.test(navigator.userAgent)) { //判断Android
		//是android系统  目前协议 安卓只能传字符串过去
		// alert("Android系统");
		jsAndroid.appPush(JSON.stringify(para))
			// jsAndroid.appPush("123456")
	} else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
		//默认为iOS系统   
		window.webkit.messageHandlers.appPush.postMessage(para);
	} else {
		//pc
		window.location.href = para.url;
		alert("PC浏览器");
	};
}
//状态标记  加载中显示
var loading = false;
//上拉刷新-监听触底事件
$(".fac-type-rightList").infinite(1).on("infinite", function () {
	if (loading) return;
	// $.showLoading();
	$('#pageNomore').addClass('displaynone');

	$('#pageLoading').show();

	loading = true;
	setTimeout(function () {
		pageIndex++;
		getList();
		// $.hideLoading();
		$('#pageLoading').hide();
		$('#pageNomore').addClass('displaynone');
	}, 1500);   //模拟延迟

});
